# $erroractionpreference = "SilentlyContinue" 
#Create the GUI Box
Function Read-MultiLineInputBoxDialog()
{
    Add-Type -AssemblyName System.Drawing
    Add-Type -AssemblyName System.Windows.Forms

# Create the Label.
    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Size(10,10) 
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.AutoSize = $true
    $label.Text = "Please enter request details as below:
Ticket;ID;HomedriveforManager(Y/N);Emailformanager(Y/N)
Blank lines should be excluded"
	
    # Create the TextBox used to capture the user's text.
    $Request = New-Object System.Windows.Forms.TextBox 
    $Request.Location = New-Object System.Drawing.Size(10,80) 
	$Request.Size = New-Object System.Drawing.Size(300,150)
    $Request.AcceptsReturn = $true
    $Request.AcceptsTab = $false
    $Request.Multiline = $true
    $Request.ScrollBars = 'Both'
    $Request.Text = "SR123456;userid;y;"

    # Create the OK button.
    $OKButton= New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(50,250)
    $OKButton.Size = New-Object System.Drawing.Size(50,25)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({ $temp= $Request.Text; $form.Close() })

	# Create the ClearText button.
    $ClearText = New-Object System.Windows.Forms.Button
    $ClearText.Location = New-Object System.Drawing.Size(120,250)
    $ClearText.Size = New-Object System.Drawing.Size(50,25)
    $ClearText.Text = "Clear"
    $ClearText.Add_Click({ $Request.Text = $null})

		# Create the Quit button.
    $QuitButton = New-Object System.Windows.Forms.Button
    $QuitButton.Location = New-Object System.Drawing.Size(200,250)
    $QuitButton.Size = New-Object System.Drawing.Size(100,25)
    $QuitButton.Text = "Quit The Script"
    $QuitButton.Add_Click({ $temp="quit"; $form.Close() })
	
    # Create the form.
    $form = New-Object System.Windows.Forms.Form 
    $form.Text = "Disable User Management"
    $form.Size = New-Object System.Drawing.Size(350,320)
    $form.FormBorderStyle = 'FixedSingle'
    $form.StartPosition = "CenterScreen"
    $form.AutoSizeMode = 'GrowAndShrink'
    $form.Topmost = $True
  # $form.AcceptButton = $AddSendButton
  # $form.CancelButton = $AddReceiveButton
    $form.ShowInTaskbar = $true

    # Add all of the controls to the form.
    $form.Controls.Add($label)
	$form.Controls.Add($Request)
	$form.Controls.Add($ClearText)
    $form.Controls.Add($OKButton)
	$form.Controls.Add($QuitButton)
	
    # Initialize and show the form.
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog() > $null   # Trash the text of the button that was clicked.

    # Return the text that the user entered.
     return $temp
}
# Update the data that user enters
Function Trim-list($stringList)
{
	$temp=@()
	foreach($item in $stringList)
	{
		if($item.trim() -ne "" -and $item.trim() -ne $null ){
			$temp +=$item.trim()
		}
	}
	return $temp
}

$res = Read-MultiLineInputBoxDialog
if ($res -ne "quit")
{
clear-content .\temp.txt
clear-content .\NoExitID.txt
$Exd=get-date
$separator ="`r`n"
$detail = $res  -split $separator
$detail  = Trim-list($detail)
if($detail.gettype().name -eq "String")	
{
	write-host -foregroundcolor "yellow" "There is only 1 user to be revoked"
}else{
	$cnts = $detail.length
	write-host -foregroundcolor "yellow" "There are totally $cnts users to be revoked"
}
$num =1 
	# $currentUserHomeDrive = ck $env:username m
	$currentUserHomeDrive = "C:\Users\IAM\disabledUser\"
	foreach ($item in $detail)
	{
		if($item.trim() -eq "" -or $item.trim() -eq $null ){
			continue
		}
	wh -foregroundcolor "yellow" "--------Starting revoking the $num user--------"
    $item = $item.split(';')
	$User=$item[1]
	$userExistStatus = gau $User
	if($userExistStatus -eq $null){
		echo "$User:NotExist" >>.\NoExitID.txt
		wh -foregroundcolor "yellow" "--------The $num user $User doesn't exist in AD system--------"
		$num=$num+1
		continue
	}
	$UserEnableStatus = ck $User enabled
	if ($UserEnableStatus -like "False"){
		wh -foregroundcolor "yellow" "The $num user $User has already been disabled previously!"
		echo "$User:PreDisabled" >>.\NoExitID.txt
		$num=$num+1
		continue
	}
	$Ticket=$item[0]
	$UserDetails = (get-aduser $User -Properties *)
	$UserGroup = cku_addedgroup $User
	$Notes = "`r`n" +$Ticket + " - Account Revoked - " +(get-date).ToString('dd/MM/yyyy')
	echo $Notes > $currentUserHomeDrive$User.txt
	echo $UserDetails >> $currentUserHomeDrive$User.txt
	echo $UserGroup  >> $currentUserHomeDrive$User.txt
	
	$Path = (Get-ADUser $User -Properties HomeDirectory).HomeDirectory
	Set-Aduser -Identity $User -Enabled $False -AccountExpirationDate $Exd
	Get-Aduser $User | Move-ADObject  –TargetPath "OU=xxx,OU=xx,OU=x,DC=domain,DC=internal"
	$count = $Groupname.count
	for ($i = 0; $i -lt [int]$count; $i++)
		{
			Remove-ADGroupMember -Identity $Groupname[$i].SamAccountName -Member $User  -Confirm:$false
		}
	$Newinfo = (get-Aduser $User -Properties info).info  +$Notes
	Set-Aduser $User -Replace @{'info'=$Newinfo }

	$mail=(get-aduser $user -Properties mail).mail
	if($mail -ne $null )
	{
		Set-Mailbox -Identity EE\$User -HiddenFromAddressListsEnabled $true
	#<----------------------Add User Manager access to  email
	$answerMail=$item[3]
		if($answerMail -eq "Y")
			{
				$UserManager =  ((Get-ADUser $User -Properties Manager).Manager |Get-ADUser).SamAccountName
				Set-Mailbox -Identity EE\$User -HiddenFromAddressListsEnabled $false
				Add-MailboxPermission -Identity $mail -User EE\$UserManager -AccessRights 'FullAccess'
				set-Mailbox -Identity $user -GrantSendOnBehalfTo @{Add=$UserManager}
			}
	}else{echo "The user $User doesn't have EA mailbox" >>temp.tx} 
#<---------------------------------------Add User Manager access to M drive
	$answerHomeDrive=$item[2]
	if($answerHomeDrive -ieq "Y")
		{
			if ((test-path $Path) -eq $false)
			{echo "The user $User doesn't have Home Drive" >>temp.txt}
			else
			{
			$UserManager =  ((Get-ADUser $User -Properties Manager).Manager |Get-ADUser).SamAccountName
			Add-NTFSAccess -Path $Path -Account EE\$UserManager -AccessRights FullControl 
			}
		}
	wh -foregroundcolor "yellow" "--------The $num  user $User has been disabled successfully!--------"
	$num=$num+1
}
wh "Each user information is saved in C:\Users\IAM\disabledUser, please refer when a disabled ID needs to be enabled"
if ((cat .\NoExitID.txt).Length -gt 0){ii .\NoExitID.txt}
if ((cat .\temp.txt).Length -gt 0){ii .\temp.txt}
}